/* 
 * Copyright WestYao.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sample_napi_wrapper.h"
#include "avcodec_log_adapter.h"
#include "xcomponent_window_wrapper.h"

using namespace OHOS::MediaAVCodec::Sample;

namespace {
constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_APP, LOG_DOMAIN_TEST, "SAMPLE_NAPI_WRAPPER"};
}

void SampleNapiWrapper::Export(napi_env env, napi_value exports)
{
    CHECK_AND_RETURN_LOG(env != nullptr && exports != nullptr, "env or exports is null");

    napi_property_descriptor desc[] = {
        { "OnSurfaceCreated", nullptr, SampleNapiWrapper::OnSurfaceCreated, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "Init", nullptr, SampleNapiWrapper::Init, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "Start", nullptr, SampleNapiWrapper::Start, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "WaitForDone", nullptr, SampleNapiWrapper::WaitForDone, nullptr, nullptr, nullptr, napi_default, nullptr }
    };

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
}

napi_value SampleNapiWrapper::OnSurfaceCreated(napi_env env, napi_callback_info info)
{
    CHECK_AND_RETURN_RET_LOG(env != nullptr && info != nullptr, nullptr, "env or info is null");

    size_t argc = 1;
    napi_value args[1] = {nullptr};
    auto ret = napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
    CHECK_AND_RETURN_RET_LOG(ret == napi_ok, nullptr, "napi_get_cb_info failed");

    uint64_t surfaceId = 0LL;
    bool lossless = true;
    ret = napi_get_value_bigint_uint64(env, args[0], &surfaceId, &lossless);
    CHECK_AND_RETURN_RET_LOG(ret == napi_ok, nullptr, "Get surface id failed");

    OHOS::MediaAVCodec::Sample::XComponentWindowWrapper::OnSurfaceCreated(surfaceId);

    return nullptr;
}

napi_value SampleNapiWrapper::Init(napi_env env, napi_callback_info info)
{
    return nullptr;
}

napi_value SampleNapiWrapper::Start(napi_env env, napi_callback_info info)
{
    return nullptr;
}

napi_value SampleNapiWrapper::WaitForDone(napi_env env, napi_callback_info info)
{
    return nullptr;
}

/*
napi_value SampleNapiWrapper::ConvertFormatContainerToNapi(napi_env env, const FormatContainerType & container)
{
    napi_value napiObj;
    napi_create_object(env, &napiObj);

    for (const auto &[key, value] : container) {
        std::string strValue = OHOS::MediaAVCodec::Sample::ValueToString(value);
        if (!strValue.empty()) {
            napi_value napiValue;
            napi_create_string_utf8(env, strValue.c_str(), strValue.size(), &napiValue);
            napi_set_named_property(env, napiObj, key.c_str(), napiValue);
        }
    }

    return napiObj;
}

napi_value SampleNapiWrapper::ConvertFormatContainerVectorToNapi(napi_env env,
                                                                 const std::vector<FormatContainerType>& containers)
{
    napi_value napiArray;
    napi_create_array(env, &napiArray);

    for (size_t i = 0; i < containers.size(); i++) {
        napi_value napiContainer = ConvertFormatContainerToNapi(env, containers[i]);
        napi_set_element(env, napiArray, i, napiContainer);
    }

    return napiArray;
}
*/
